package by.ins.app;

import java.util.List;

import org.apache.log4j.Logger;

import by.ins.app.commands.AppCommnandProvider;
import by.ins.app.console.RemoteAdminConsoleImpl;
import by.yativem.commons.console.ConsoleCommandBean;
import by.yativem.commons.task.AbstractCallableTaskCommand;
import by.yativem.commons.task.AbstractManagedTask;
import by.yativem.commons.task.TasksEngine;

public class App extends AbstractManagedTask {
	private static final Logger log = Logger.getLogger(App.class);
	private AppCommnandProvider appCommnandProvider;
	private TasksEngine tasksEngine;
	private List<ConsoleCommandBean> startUpAppCommands;
	private RemoteAdminConsoleImpl remoteAdminConsoleImpl;
	private boolean runStartUpCommand = false;

	public App() {
		this.taskId = -1;
		this.taskName = "INS App";
		this.taskDescription = "Main task of INS App";

	}

	@Override
	public void doTask() {
		// adminConsole = new AdminConsole(this);
		// appCommnandProvider.start();
		try {
			
			/* start task engine */
			Thread engineThread = new Thread(getTasksEngine());
			engineThread.start();

			if (remoteAdminConsoleImpl != null) {
				remoteAdminConsoleImpl.init();
			}

			if ( runStartUpCommand && startUpAppCommands != null && !startUpAppCommands.isEmpty()) {

				Runnable runnable = new Runnable() {

					@Override
					public void run() {
						for (ConsoleCommandBean commandBean : startUpAppCommands)
							appCommnandProvider.sendCommand(commandBean);
					}
				};
				 Thread th = new Thread(runnable);
				 th.setName("App start up commands");
				 th.start();

				/*
				 * for (ConsoleCommandBean commandBean :startUpAppCommands)
				 * appCommnandProvider.sendCommand(commandBean);
				 */
			}

			while (true) {
				checkAllCommands();
				synchronized (this.locker) {
					this.setTaskStatus(TaskStatus.WAIT);
					this.locker.wait(1000);
					this.setTaskStatus(TaskStatus.RUNNING);
				}
			}

		} catch (Throwable e) {
			log.error(e.getMessage(), e);
		}

	}

	@Override
	protected void putCallableCommand(AbstractCallableTaskCommand<?, AbstractManagedTask> callableCommand) {
		super.putCallableCommand(callableCommand);
		synchronized (this.locker) {
			this.locker.notifyAll();
		}
	}

	public TasksEngine getTasksEngine() {
		return tasksEngine;
	}

	public void setTasksEngine(TasksEngine tasksEngine) {
		this.tasksEngine = tasksEngine;
	}

	public AppCommnandProvider getAppCommnandProvider() {
		return appCommnandProvider;
	}

	public void setAppCommnandProvider(AppCommnandProvider appCommnandProvider) {
		this.appCommnandProvider = appCommnandProvider;
	}

	public List<ConsoleCommandBean> getStartUpAppCommands() {
		return startUpAppCommands;
	}

	public void setStartUpAppCommands(List<ConsoleCommandBean> startUpAppCommands) {
		this.startUpAppCommands = startUpAppCommands;
	}

	public RemoteAdminConsoleImpl getRemoteAdminConsoleImpl() {
		return remoteAdminConsoleImpl;
	}

	public void setRemoteAdminConsoleImpl(RemoteAdminConsoleImpl remoteAdminConsoleImpl) {
		this.remoteAdminConsoleImpl = remoteAdminConsoleImpl;
	}

	public void startApp() {
		if( this.getTaskStatus() != AbstractManagedTask.TaskStatus.NO_STARTED){
			throw new IllegalStateException();
		}
		Thread th = new Thread(this);
		th.setName(this.getTaskName());
		th.start();
	}

	public boolean isRunStartUpCommand() {
		return runStartUpCommand;
	}

	public void setRunStartUpCommand(boolean runStartUpCommand) {
		this.runStartUpCommand = runStartUpCommand;
	}
	
	

}
